#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>

#define N 1024

int main() {
    // 原始读取端代码（唯一main函数）
    int fd;
    char buf[N];
    
    if (mkfifo("./mypipe", 0666) == -1 && errno != EEXIST) {
        perror("mkfifo failed");
        exit(EXIT_FAILURE);
    }

    printf("Reader waiting...\n");
    if ((fd = open("./mypipe", O_RDONLY)) == -1) {
        perror("open failed");
        exit(EXIT_FAILURE);
    }

    ssize_t bytes = read(fd, buf, N-1);
    if (bytes > 0) {
        buf[bytes] = '\0';
        printf("Received: %s\n", buf);
    } else {
        perror("read error");
    }

    close(fd);
    return EXIT_SUCCESS;
}
